package com.haixiaoke.saas.manage.department.controller;

import javax.servlet.http.HttpServletResponse;

import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.department.domain.ElectronicVisa;
import com.haixiaoke.saas.department.domain.ElectronicVisaRecord;
import com.haixiaoke.saas.department.service.IElectronicVisaService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 企业电子签信息Controller
 *
 * @author panda
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/manage/department/electronicVisa")
public class ManageElectronicVisaController extends BaseController {
    @Autowired
    private IElectronicVisaService electronicVisaService;

    /**
     * 查询企业电子签信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:electronicVisa:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElectronicVisa electronicVisa) {
        startPage();
        List<ElectronicVisa> list = electronicVisaService.selectElectronicVisaList(electronicVisa);
        return getDataTable(list);
    }

    /**
     * 导出企业电子签信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:electronicVisa:export')")
    @Log(title = "企业电子签信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElectronicVisa electronicVisa) {
        List<ElectronicVisa> list = electronicVisaService.selectElectronicVisaList(electronicVisa);
        ExcelUtil<ElectronicVisa> util = new ExcelUtil<ElectronicVisa>(ElectronicVisa.class);
        util.exportExcel(response, list, "企业电子签信息数据");
    }

    /**
     * 获取企业电子签信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:electronicVisa:query')")
    @GetMapping(value = "/{visaId}")
    public AjaxResult getInfo(@PathVariable("visaId") Long visaId) {
        return success(electronicVisaService.selectElectronicVisaByVisaId(visaId));
    }

    /**
     * 添加企业电子合同数量
     */
    @PreAuthorize("@ss.hasPermi('manage:electronicVisa:edit')")
    @Log(title = "企业电子签信息", businessType = BusinessType.UPDATE)
    @PutMapping("/add/contractNumber")
    public AjaxResult addContractNumber(@RequestBody ElectronicVisaRecord electronicVisaRecord) {
        if (electronicVisaRecord.getVisaNum() <= 0) {
            return error("添加合同数量需要大于0");
        }
        return toAjax(electronicVisaService.addVisaContractNumber(electronicVisaRecord));
    }

    /**
     * 扣减企业电子合同数量
     */
    @PreAuthorize("@ss.hasPermi('manage:electronicVisa:edit')")
    @Log(title = "企业电子签信息", businessType = BusinessType.UPDATE)
    @PutMapping("/subtract/contractNumber")
    public AjaxResult subtractContractNumber(@RequestBody ElectronicVisaRecord electronicVisaRecord) {
        if (electronicVisaRecord.getVisaNum() <= 0) {
            return error("扣减合同数量需要大于0");
        }
        return toAjax(electronicVisaService.subtractVisaContractNumber(electronicVisaRecord));
    }

    /**
     * 电子签授权认证
     */
    @PreAuthorize("@ss.hasPermi('manage:electronicVisa:sendSmsAuth')")
    @Log(title = "企业电子签信息", businessType = BusinessType.UPDATE)
    @PutMapping("/sendSmsAuth/{tenantId}")
    public AjaxResult sendSmsAuth(@PathVariable("tenantId") String tenantId) {
        return electronicVisaService.sendSmsAuth(tenantId);
    }
}
